package io;


import java.io.FileInputStream;
import java.io.ObjectInputStream;

/**
 * 包装流-ObjectInputStream
 */
public class ObjectInputStream_ {

    public static void main(String[] args) throws Exception {

        String filePath = "D:\\IdeaCode\\javaStudy\\data.txt";

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));

        //读取
        //读取(反序列化)的顺序需要和保存(序列化)数据的顺序一致

        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());

        //o 编译类型 Object 运行类型 Dog
        Object o = ois.readObject();
        System.out.println(o.getClass());   //底层 Object -> Dog
        System.out.println(o);

        //如果希望调用Dog方法，需要向下转型
//        Dog d = (Dog) o;

        //关闭流，底层会自动关闭内层节点流
        ois.close();
    }
}
